package com.jinzhuo.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinzhuo.music.domain.Singer;
import com.jinzhuo.music.service.SingerService;
import com.jinzhuo.music.utils.Consts;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@RestController
@RequestMapping("/singer")
public class SingerController {
    @Autowired
    private SingerService singerService;

    /**
     * 添加歌手
     */
    @RequestMapping(value = "addSinger",method = RequestMethod.POST)
    public Object addSinger(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String name = request.getParameter("name").trim();
        String sex = request.getParameter("sex").trim();
        String pic = request.getParameter("pic").trim();
        String birth = request.getParameter("birth").trim();
        String location = request.getParameter("location").trim();
        String introduction = request.getParameter("introduction").trim();
        //把生日转换成data形式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
             date = dateFormat.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //保存到歌手的对象中
        Singer singer = new Singer();
        singer.setName(name);
        singer.setBirth(date);
        singer.setIntroduction(introduction);
        singer.setLocation(location);
        singer.setPic(pic);
        singer.setSex(new Byte(sex));
        boolean flag = singerService.addSinger(singer);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"添加成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"添加失败");
        }
        return object;
    }

    /**
     * 修改歌手
     */
    @RequestMapping(value = "updateSinger",method = RequestMethod.POST)
    public Object updateSinger(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String id = request.getParameter("id").trim();
        String name = request.getParameter("name").trim();
        String sex = request.getParameter("sex").trim();
//        String pic = request.getParameter("pic").trim();
        String birth = request.getParameter("birth").trim();
        String location = request.getParameter("location").trim();
        String introduction = request.getParameter("introduction").trim();
        //把生日转换成data形式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = dateFormat.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //保存到歌手的对象中
        Singer singer = new Singer();
        singer.setId(Integer.parseInt(id));
        singer.setName(name);
        singer.setBirth(date);
        singer.setIntroduction(introduction);
        singer.setLocation(location);
//        singer.setPic(pic);
        singer.setSex(new Byte(sex));
        boolean flag = singerService.updateSinger(singer);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"修改成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"修改失败");
        }
        return object;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "deleteSinger",method = RequestMethod.POST)
    public Object deleteSinger(HttpServletRequest request) {
        JSONObject object = new JSONObject();
        String id = request.getParameter("id").trim();
        boolean flag = singerService.deleteSinger(Integer.parseInt(id));
        return flag;
    }

    /**
     * 据主键查询单个对象
     */
    @RequestMapping(value = "getSinger",method = RequestMethod.POST)
    public Object getSinger(HttpServletRequest request) {
        String id = request.getParameter("id").trim();
        Singer singer = singerService.getSinger(Integer.parseInt(id));
        return singer;
    }

    /**
     * 查询所有对象
     */
    @RequestMapping(value = "queryAllSinger",method = RequestMethod.POST)
    public Object queryAllSinger(HttpServletRequest request) {
//        String id = request.getParameter("id").trim();
        return singerService.queryAllSinger();
    }

    /**
     * 根据名字查询歌手
     */
    @RequestMapping(value = "querySingerByName",method = RequestMethod.POST)
    public Object querySingerByName(HttpServletRequest request) {
        String name =request.getParameter("name").trim();
        List<Singer> singers = singerService.querySingerByName("%"+name+"%");
        return singers;
    }

    /**
     * 根据性别查询歌手
     */
    @RequestMapping(value = "querySingerBySex",method = RequestMethod.POST)
    public Object querySingerBySex(HttpServletRequest request) {
        String sex =request.getParameter("sex").trim();
        List<Singer> singers = singerService.querySingerBySex(Integer.parseInt(sex));
        return singers;
    }
    /**
     * 更新歌手头像
     *
     */
    @RequestMapping(value = "updateSingerPic",method = RequestMethod.POST)
    public Object updateSingerPic(@RequestParam("file")MultipartFile avatorFile,@RequestParam("id")int id){
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"头像上传失败");
            return jsonObject;
        }
        //文件名=当前时间的毫秒+原来的文件名
        String fileName=System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径
        String filePath=System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"singerPic";
        //如果文件路径不存在，新增路径
        File file = new File(filePath);
        if (!file.exists()){
            file.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/singerPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatorPath);
            boolean flag = singerService.updateSinger(singer);
            if (flag){
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"头像上传成功");
                return jsonObject;
            }else {
                jsonObject.put(Consts.CODE,0);
                jsonObject.put(Consts.MSG,"上传失败");
                return jsonObject;
            }
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());
        }finally {
            return jsonObject;
        }
    }
}
